package com.flyee.viewer
{
	import com.flyee.controller.UIController;
	import com.flyee.events.SelectionEvent;
	import com.flyee.model.MenuData;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.GenerateIDTool;
	import com.flyee.utils.depot.UIDepot;
	import com.flyee.viewer.menu.OptionCell;
	import com.flyee.viewer.menu.RealtyOption;
	
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.core.UIComponent;

	
	public class MainMenuViewer extends UIComponent implements Destroy
	{
		public static var NAME:String = "com.flyee.viewer.MainMenuViewer";
		public var vid : String = GenerateIDTool.generateID();
		
		private const WIDTH_BAR:int = 396;
		private const HEIGHT_BAR:int = 35;

		private var realtyBG:Bitmap = null;
		private var hypoBGMC:MovieClip = null;
		private var mainSelectBG:Bitmap = null;
		private var hypoSeleetBG:Bitmap = null;

		private var iconSprite:Sprite = null;
		private var mainSprite:Sprite = null;
		private var hypoSprite:Sprite = null;
		private var realtyOption:RealtyOption = null;
		private var oldCell:OptionCell = null;
		private var oldHypoCell:OptionCell = null;
		private var iconCellList:Array = null;
		private var txtCellList:Array = null;
		private var _configUrl:String = "resource/ui_land.xml";

		public function MainMenuViewer()
		{
			super();
			this.mouseChildren = true;
			this.mouseEnabled = true;
			init(_configUrl);
		}
		
		private function init(url:String):void
		{
			realtyOption = new RealtyOption();
			realtyOption.addEventListener(Event.COMPLETE, completeHandler);
			realtyOption.xmlLoad(url);

			realtyBG = new Bitmap();
			mainSelectBG = new Bitmap();
			hypoSeleetBG = new Bitmap();

			hypoBGMC = UIDepot.getInstance().getMovieClipByName("HypoBGMC");
			realtyBG.bitmapData = UIDepot.getInstance().getBitmapDataByName("RealtyBG");
			mainSelectBG.bitmapData = UIDepot.getInstance().getBitmapDataByName("MainSelectBG");
			hypoSeleetBG.bitmapData = UIDepot.getInstance().getBitmapDataByName("HypoSelectBG");

			hypoBGMC.x = 23;
			hypoBGMC.y = 3;
			hypoBGMC.gotoAndStop(1);
			this.addChild(hypoBGMC);
			this.addChild(realtyBG);
			initBG();
			this.addEventListener(MouseEvent.MOUSE_UP, stopDragHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, startDragHandler);
			this.x = (ObjectCollection.uiContainer.width - this.WIDTH_BAR)/2;
			this.y = ObjectCollection.uiContainer.height - this.HEIGHT_BAR - this.HEIGHT_BAR;
		}

		private function initBG():void
		{
			iconSprite = new Sprite();
			mainSprite = new Sprite();
			hypoSprite = new Sprite();
			iconCellList = new Array(6);
			txtCellList = new Array(6);

			iconSprite.name = "icon";
			mainSprite.name = "main";
			hypoSprite.name = "hypo";

			iconSprite.x = 43;
			iconSprite.y = 3;

			mainSprite.x = 43;
			mainSprite.y = 38;

			hypoSprite.x = 43;
			hypoSprite.y = 3;

			drawBG(iconSprite.graphics);
			drawBG(hypoSprite.graphics);
			drawBG(mainSprite.graphics);
		
			this.addChild(iconSprite);
			this.addChild(mainSprite);
		}

		private function completeHandler(e:Event):void
		{
			addButtonToBar();
		}

		private function drawBG(graph:Graphics):void
		{
			graph.clear();
			graph.beginFill(0x0000ff, 0);
			graph.drawRect(0, 0, WIDTH_BAR, HEIGHT_BAR);
			graph.endFill();
		}

		private function stopDragHandler(e:MouseEvent):void
		{
			this.stopDrag();
			var point:Point = new Point(this.x, this.y);
			//CookObject.getInstance().saveDataByType(CookObject.MENU_WIN, point);
		}

		private function startDragHandler(e:MouseEvent):void
		{
			this.startDrag();
			var uiController:UIController = ObjectCollection.beansContext.getObject("uiController") as UIController;
			uiController.swapPanelToTop(this);
		}

		/**
		 * 主菜单的选择事件
		 * @param e
		 *
		 */
		private function mainSelectHandler(e:SelectionEvent):void
		{
			var cell:OptionCell = e.currentTarget as OptionCell;

			if (cell.isIconCell)
			{
				cell = synOperation(cell.cellID);
			}

			if (oldCell)
			{
				hypoBGMC.gotoAndStop(1);

				if (oldCell.cellID != cell.cellID)
				{
					oldCell.showSelectBG(null);
					if(cell.hasChildren){
						cell.showSelectBG(mainSelectBG.bitmapData.clone());					
						changeBG(cell.cellID);
					}else{
						if (this.getChildByName("hypo"))
						{
							this.removeChild(hypoSprite);	
						}
						this.addChild(iconSprite);
					}
					oldCell = cell;
				}
				else
				{
					if (this.getChildByName("hypo"))
					{
						this.removeChild(hypoSprite);	
					}
					this.addChild(iconSprite);
					oldCell.showSelectBG(null);
					oldCell = null;
				}
			}
			else
			{
				cell.showSelectBG(mainSelectBG.bitmapData.clone());
				changeBG(cell.cellID);
				oldCell = cell;
			}

			if (cell.hasChildren)
			{

			}
			else
			{
				cell.showSelectBG(null);
			}
			realtyOption.openWindow(cell.cellKeywords);
		}

		private function synOperation(id:int):OptionCell
		{

			for(var i:int = 0; i < 6; i++)
			{
				var cell:OptionCell = txtCellList[i]as OptionCell;

				if (cell.cellID == id)
				{
					return cell;
				}
			}
			return null;
		}

		/**
		 * 次菜单的选择事件
		 * @param e
		 *
		 */
		private function hypoSeclectHandler(e:SelectionEvent):void
		{
			var cell:OptionCell = e.currentTarget as OptionCell;

			if (oldHypoCell)
			{

				if (oldHypoCell.cellID != cell.cellID)
				{
					oldHypoCell.showSelectBG(null);
					cell.showSelectBG(hypoSeleetBG.bitmapData.clone());
				}
			}
			else
			{
				cell.showSelectBG(hypoSeleetBG.bitmapData.clone());
			}
			oldHypoCell = cell;
			realtyOption.openWindow(cell.cellKeywords);
		}

		private function overHandler(e:MouseEvent):void
		{
			var cell:OptionCell = e.currentTarget as OptionCell;

			if (cell.cellID > 10)
			{
				cell.showSelectBG(hypoSeleetBG.bitmapData.clone());
			}
		}

		private function outHandler(e:MouseEvent):void
		{
			var cell:OptionCell = e.currentTarget as OptionCell;

			if (cell.cellID > 10)
			{
				cell.showSelectBG(null);
			}
		}

		private function removeHandler(e:Event):void
		{
		}

		private function changeBG(tag:int):void
		{
			var list:Array = realtyOption.getListAt(tag);
			var id:int = 0;

			while(hypoSprite.numChildren > 0)
			{
				hypoSprite.removeChildAt(0);
			}

			if (list.length > 0)
			{
				hypoBGMC.play();
				//hypoBGMC.mouseEnabled = false;
				hypoBGMC.enabled = false;
				for(var i:int = 0; i < list.length; i++)
				{
					id = tag * 10 + (i + 1);
					var model:MenuData = MenuData(list[i]);
					var cell:OptionCell = new OptionCell(model.itemId, i, model.itemKeyword);

					cell.setTipInfo(model.itemName);
					cell.addEventListener(SelectionEvent.SELECTED, hypoSeclectHandler);
					cell.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
					cell.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
					hypoSprite.addChild(cell);
				}

				if (this.getChildByName("icon"))
				{
					this.removeChild(iconSprite);
					this.addChild(hypoSprite);
				}
			}
		}

		private function addButtonToBar():void
		{
			var list:Array = realtyOption.getMainList();

			for(var i:int = 0; i < list.length; i++)
			{
				var model:MenuData = MenuData(list[i]);
				var cell:OptionCell = new OptionCell(model.itemId, i, model.itemKeyword);
				var icon:OptionCell = new OptionCell(model.itemId, i, model.itemKeyword, true);
				icon.setIconButton(UIDepot.getInstance().getSimpleButtonByName(model.itemIcon));
				cell.setTipInfo(model.itemName);
				cell.hasChildren = model.isLeaf;
				iconCellList[i] = icon;
				txtCellList[i] = cell;
				icon.addEventListener(SelectionEvent.SELECTED, mainSelectHandler);
				cell.addEventListener(SelectionEvent.SELECTED, mainSelectHandler);
				iconSprite.addChild(icon);
				mainSprite.addChild(cell);
			}
		}
		
		public function set configURL(url:String):void{
			dispose();
			_configUrl = url;
			init(url);
		}

		public function dispose():void
		{
			while(this.numChildren > 0)
			{
				this.removeChildAt(0);
			}

			while(iconSprite.numChildren > 0)
			{
				iconSprite.removeChildAt(0);
			}
			iconSprite = null;

			while(mainSprite.numChildren > 0)
			{
				mainSprite.removeChildAt(0);
			}
			mainSprite = null;

			while(hypoSprite.numChildren > 0)
			{
				hypoSprite.removeChildAt(0);
			}

			if (realtyBG.bitmapData != null)
			{
				realtyBG.bitmapData.dispose();
				realtyBG.bitmapData = null;
			}
			realtyBG = null;
			hypoBGMC = null;

			if (mainSelectBG.bitmapData != null)
			{
				mainSelectBG.bitmapData.dispose();
				mainSelectBG.bitmapData = null;
			}
			mainSelectBG = null;

			if (hypoSeleetBG.bitmapData != null)
			{
				hypoSeleetBG.bitmapData.dispose();
				hypoSeleetBG.bitmapData = null;
			}
			hypoSeleetBG = null;


			if (oldCell != null)
			{
				oldCell.clear();
			}
			oldCell = null;

			if (oldHypoCell != null)
			{
				oldHypoCell.clear();
			}
			oldHypoCell = null;
			hypoSprite = null;

			realtyOption = null;
			var cell:OptionCell = null;

			if (iconCellList != null)
			{

				for(var j:int = 0; j < iconCellList.length; j++)
				{
					cell = iconCellList[j]as OptionCell;

					if (cell)
					{
						cell.clear();
						cell = null;
					}
					iconCellList.splice(j, 1);
				}
			}
			iconCellList = null;

			if (txtCellList != null)
			{

				for(var i:int = 0; i < txtCellList.length; i++)
				{
					cell = txtCellList[i]as OptionCell;

					if (cell)
					{
						cell.clear();
						cell = null;
					}
					txtCellList.splice(i, 1);
				}
			}
			txtCellList = null;
		}
	}
}